﻿using OracleDemo;
using System;
using System.Data;
using System.Windows.Forms;

namespace linkToOracle
{
    public partial class MytestFrm : Form
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        public MytestFrm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 校验数据连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string sqlString = txtsql.Text.Trim();
            try
            {
                OracleHelper db = new OracleHelper();
                DataTable dt = db.ExecuteDataTable(sqlString);
                if (dt != null)
                {
                    dgv.DataSource = null;
                    FilldataGridView(dt);
                }
            }
            catch (Exception ex)
            {
                txtlog.Text = ex.Message;
            }
        }

        /// <summary>
        /// FilldataGridView
        /// </summary>
        private void FilldataGridView(DataTable MyDt)
        {
            DataTable dt = MyDt;
            foreach (DataColumn col in dt.Columns)
            {
                dgv.Columns.Add(col.ColumnName, col.ColumnName);
            }
            dgv.Rows.Clear();
            dgv.Rows.Add(dt.Rows.Count);
            int i = 0;
            foreach (DataRow row in dt.Rows)
            {
                DataGridViewRow r1 = dgv.Rows[i];
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    r1.Cells[j].Value = row[j].ToString();
                }
                i++;
            }
        }
    }
}
